import random
import re
import Entity
from Game import*
from Map import*


class IA_player(object):
    
    
    def direction_possible(self,x,y,tentative,map): 
        choix=[]
        x=int(x)
        y=int(y)
        infodir=map.getInfo(x,y)
        tentative[x][y]=False
        if infodir[0]=='zombie'or infodir[0]=='water' or infodir[0]=='OurZombie':
            tentative[x-1][y]=False
        else:
            if tentative[x-1][y]==True:
                choix.append('N')
        if infodir[1]=='zombie'or infodir[1]=='water' or infodir[1]=='OurZombie':
            tentative[x+1][y]=False
        else:
            if tentative[x+1][y]==True:
                choix.append('S')
        if infodir[2]=='zombie'or infodir[2]=='water' or infodir[2]=='OurZombie':
            tentative[x][y+1]=False
        else:
            if tentative[x][y+1]==True:
                choix.append('E')
        if infodir[3]=='zombie'or infodir[3]=='water' or infodir[3]=='OurZombie':
            tentative[x][y-1]=False
        else:
            if tentative[x][y-1]==True:
                choix.append('W')
       
        return choix
    
    def strategie(self, data, map):
        self.data = data
        tentative=[[True for x in xrange(map.getRow())] for x in xrange(map.getCol())]
        zombies = []
        choice=[]
        
           
        for m in re.finditer(r"zombie\s+(\d+)", ''.join(data)):
            print 'on a le zombie',m
            z_id = int(m.group(1))
            if z_id not in zombies:
                zombies.append(z_id)

        for m in re.finditer(r"contagion\s+(\d+)\s+(\d+)", ''.join(data)):
            z_id = int(m.group(2))
            if z_id not in zombies:
                zombies.append(z_id)

        for m in re.finditer(r"dead\s+(\d+)", ''.join(data)):
            z_id = int(m.group(1))
            if z_id in zombies:
                zombies.remove(z_id)
        
    # Sends moves for zombies
        for z in zombies:
            trouve=False
            zombie_position = [0,0]
            z_id = int(m.group(1))
            
            for i in self.data :
                data_splitzombie = i.split(' ')
                if data_splitzombie[0] == 'zombie' : 
                    if int(data_splitzombie[1]) == z:
                        zombie_position = [data_splitzombie[2], data_splitzombie[3]]
                        print 'Zombie : id : ' ,data_splitzombie[1],' row : ',zombie_position[0],' col : ',zombie_position[1]
                if data_splitzombie[0] == 'contagion' : 
                    if int(data_splitzombie[2]) == z:
                        zombie_position = [data_splitzombie[3], data_splitzombie[4]]
                        print 'Zombie : id : ' ,data_splitzombie[2],' row : ',zombie_position[0],' col : ',zombie_position[1]
                        
            
            choix = self.direction_possible(zombie_position[0],zombie_position[1],tentative,map)
#if the zombie is at the south of the map we move he on the north if he can
            if zombie_position[0]>map.getRow()/2:
                for i in choix:
                    if i=='N':
                        trouve=True
                if trouve==True:
                    dir_rand='N'
                    trouve=False
#Or we look if he is at the east and we move he on the west
                else :
                    if zombie_position[1]>map.getCol()/2:
                        for i in choix:
                            if i == 'W':
                                trouve=True
                        if trouve==True:
                            dir_rand='W'
                            trouve=False
                    else:
                        for i in choix:
                            if i =='E':
                                trouve=True
                        if trouve==True:
                            dir_rand='E'
                            trouve=False
            else:
                for i in choix:
                    if i =='S':
                        trouve=True
                if trouve==True:
                    dir_rand='S'
                    trouve=False
                else:
                    dir_rand=random.choice(choix)
            print 'LIA a decide :' , dir_rand
            choice.append('move %d %s' % (z, dir_rand))
            dir_rand=random.choice(choix)
            print 'choix random : ',dir_rand
        print 'Liste des actions du tour : ' ,choice   

        choice.append('end')
        
        return choice
